package com.ts.api.module.api.convert.receive.entity;

import com.gitee.apanlh.util.base.BigDecimalUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.common.constant.api.ApiEnum;
import com.ts.api.common.redis.ApiMatchRedisKey;
import com.ts.api.common.util.H5DomainUrlUtils;
import com.ts.api.module.api.entity.bo.ApiDataExchangeBO;
import com.ts.api.module.api.entity.po.ApiKeyPO;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeField;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributePhoneMd5Field;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;

/**
 *  撞库返回业务消息参数
 *
 *  @author Pan
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ApiReceivePhoneMd5DataExchange extends ApiReceive {

    /** 请求id */
    private String reqId;
    /** API渠道 */
    private ApiKeyPO apiKeyPO;
    /** 原价格 */
    private BigDecimal price;
    /** 机构名称 */
    private String orgName;
    /** logo地址 */
    private String logoPath;
    /** 投放名称 */
    private String displayName;
    /** 渠道分成后价格(原价*分成比例) */
    private BigDecimal channelSharePrice;
    /** 该用户是否本地存在 */
    private boolean hasLocalExist;
    /** md5模式 */
    private String phoneMd5;
    /** 半流程模式-回调url */
    private String callUrl;

    /**
     *  创建撞库时候-唯一编码（订单号），将保存至redis中
     *  (默认保存reqId+phoneMd5)
     *  @return String  返回key
     */
    public String createUniqueId() {
        String key = ApiMatchRedisKey.getKey(this.reqId, this.phoneMd5);
        ApiMatchRedisKey.API_CHANNEL_DATA_EXCHANGE_REQ_ID.setForMinutes(key, key, 10);
        return key;
    }

    /**
     *  创建撞库时候-唯一编码（订单号），将保存至redis中
     *  @param  value   需要保存的值
     *  @return String  返回key
     */
    public String createUniqueId(String value) {
        String key = ApiMatchRedisKey.getKey(this.reqId, this.phoneMd5);
        ApiMatchRedisKey.API_CHANNEL_DATA_EXCHANGE_REQ_ID.setForMinutes(key, value, 10);
        return key;
    }

    public static ApiReceivePhoneMd5DataExchange suc() {
        ApiReceivePhoneMd5DataExchange apiReceivePhoneMd5DataExchange = new ApiReceivePhoneMd5DataExchange();
        apiReceivePhoneMd5DataExchange.setBizMsg(ApiEnum.API_SIGN_SUC.getMsg());
        apiReceivePhoneMd5DataExchange.setBizCode(ApiEnum.API_SIGN_SUC.getCode());
        return apiReceivePhoneMd5DataExchange;
    }

    public static ApiReceivePhoneMd5DataExchange fail(String msg, Integer code) {
        ApiReceivePhoneMd5DataExchange apiReceivePhoneMd5DataExchange = new ApiReceivePhoneMd5DataExchange();
        apiReceivePhoneMd5DataExchange.setBizMsg(msg);
        apiReceivePhoneMd5DataExchange.setBizCode(code);
        return apiReceivePhoneMd5DataExchange;
    }

    /**
     *  创建撞库消息业务参数
     *
     *  @param  apiDataExchangeBO   API撞库实体
     *  @return ApiReceiveDataExchangeMessage
     */
    public static ApiReceivePhoneMd5DataExchange create(ApiDataExchangeBO apiDataExchangeBO, ApiKeyPO apiKeyPO) {
        ApiReceivePhoneMd5DataExchange message = suc();
        //  返回字段
        PlanDistributeField planDistributeField = apiDataExchangeBO.getPlanDistributeField();
        PlanDistributePhoneMd5Field phoneMd5Field = planDistributeField.getPhoneMd5Field();

        message.setApiKeyPO(apiKeyPO);
        message.setPrice(phoneMd5Field.getPrice());
        message.setLogoPath(phoneMd5Field.getLogoPath());
        message.setOrgName(phoneMd5Field.getProductName());
        message.setDisplayName(phoneMd5Field.getDisplayName());
        message.setPhoneMd5(phoneMd5Field.getPhoneMd5());
        message.setReqId(phoneMd5Field.getReqId());

        //  回调授权页面-如果匹配非平台则使用自己的回调授权页，如果匹配平台计划则返回的是下游授权页面
        if (ValidParam.isEmpty(phoneMd5Field.getCallUrl())) {
            String h5CallUrl = H5DomainUrlUtils.createH5CallUrl(phoneMd5Field.getPhoneMd5(), apiKeyPO.getApiClientId());
            message.setCallUrl(h5CallUrl);
        } else {
            message.setCallUrl(phoneMd5Field.getCallUrl());
        }

        // 渠道分成后的结算价格
        BigDecimal apiShare = apiKeyPO.getApiShare();
        if (!Eq.str("0.00", String.valueOf(apiShare))) {
            BigDecimal quota = BigDecimalUtils.division(apiShare, 100);
            BigDecimal channelSharePrice = BigDecimalUtils.multiply(phoneMd5Field.getPrice(), quota);
            message.setChannelSharePrice(channelSharePrice);
        } else {
            message.setChannelSharePrice(phoneMd5Field.getPrice());
        }
        return message;
    }

}
